<template>
  <div>
    <!-- 统计展示 -->
    <el-row :gutter="20">
      <!-- 骨架屏 -->
      <template v-if="panels.length == 0">
        <el-col :span="6" v-for="i in 4" :key="i">
          <el-skeleton style="width: 100%" animated loading>
            <template #template>
              <el-card shadow="hover" class="border-0">
                <template #header>
                  <div class="flex justify-between">
                    <el-skeleton-item variant="text" style="width: 50%" />
                    <el-skeleton-item variant="text" style="width: 10%" />
                  </div>
                </template>
                <el-skeleton-item variant="h3" style="width: 80%" />
                <el-divider />
                <div class="flex justify-between text-sm text-gray-500">
                  <el-skeleton-item variant="text" style="width: 50%" />
                  <el-skeleton-item variant="text" style="width: 10%" />
                </div>
              </el-card>
            </template>
          </el-skeleton>
        </el-col>
      </template>
      <!-- 内容卡片 -->
      <el-col
        :span="6"
        :offset="0"
        v-for="(item, index) in panels"
        :key="index"
      >
        <el-card shadow="hover" class="border-0">
          <template #header>
            <div class="flex justify-between">
              <!-- 内容标题 -->
              <span class="text-sm">{{ item.title }}</span>
              <!-- 统计周期 -->
              <el-tag :type="item.unitColor" effect="plain">
                {{ item.unit }}
              </el-tag>
            </div>
          </template>
          <!-- 数据展示 -->
          <span class="text-3xl font-bold text-gray-500">
            <!-- {{ item.value }} -->
            <CountTo :value="item.value" />
          </span>
          <el-divider />
          <div class="flex justify-between text-sm text-gray-500">
            <!-- 内容扩充 -->
            <span>{{ item.subTitle }}</span>
            <!-- 扩充数据 -->
            <span>{{ item.subValue }}</span>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <!-- 分类卡片 -->
    <Classification />
    <!-- 图表和提示组件 -->
    <el-row :gutter="20" class="mt-5">
      <!-- 图表展示 -->
      <el-col :span="12" :offset="0">
        <IndexChart />
      </el-col>
      <!-- 提示卡片 -->
      <el-col :span="12" :offset="0">
        <IndexCard
          title="店铺及商品提示"
          tip="店铺及商品提示"
          :btns="goods"
          class="mb-4"
        />
        <IndexCard
          title="交易提示"
          tip="需要立即处理的交易订单"
          :btns="order"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { getStatistics1, getStatistics2 } from '@/api/index.js'
import { ref } from 'vue'
import { onMounted } from 'vue'
import CountTo from '@/components/CountTo.vue'
import Classification from '@/components/Classification.vue'
import IndexChart from '@/components/IndexChart.vue'
import IndexCard from '../components/IndexCard.vue'

const panels = ref([])
// getStatistics1().then(res => {
//   panels.value = res.panels
// })
// 创建一个函数来处理数据获取和定时器
function fetchDataWithDelay () {
  setTimeout(() => {
    getStatistics1().then(res => {
      panels.value = res.panels
    })
  }, 1000) // 等待1000毫秒（1秒）
}
// 在组件挂载时调用函数
onMounted(() => {
  fetchDataWithDelay()
})

// 提示卡片数据
const goods = ref([])
const order = ref([])
getStatistics2().then(res => {
  goods.value = res.goods
  order.value = res.order
})
</script>

<style scoped></style>
